const path = require("path");
const Excel = require("exceljs");
const multer = require("multer");
// 设置存储配置
// 设置 multer 存储配置（这里不设置，默认内存存储）
const upload = multer({ dest: "uploads/" });


function getFileInterface(app) {
  // 处理单个文件上传
  app.post("/upload-single", upload.single("file"), (req, res) => {
    const file = req.file;
    if (!file) {
      return res.status(400).send("No file uploaded.");
    }
    res.send("File uploaded successfully.");
  });

  // 处理多个文件上传
  app.post("/upload-multiple", upload.array("files", 12), (req, res) => {
    const files = req.files;
    if (!files) {
      return res.status(400).send("No files uploaded.");
    }
    res.send("Files uploaded successfully.");
  });

  // 发送本地文件docx
  app.get("/getFile", (req, res) => {
    res.sendFile(__dirname + "/旅游日记.docx");
  });
  // 下载接口
  app.get("/download", async function (req, res) {
    const workbook = new Excel.Workbook();
    await workbook.xlsx.readFile("./成绩表.xlsx"); // 替换为你的Excel文件路径

    // 将Excel文件流发送到客户端
    res.setHeader(
      "Content-Type",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    );
    res.setHeader("Content-Disposition", "attachment; filename=file.xlsx");
    workbook.xlsx.write(res).then(() => res.end());
  });
  // 获取10万条数据
  app.get("/getList", function (req, res) {
    res.header("Access-Control-Allow-Origin", "*"); // 允许跨域
    let arr = []; // 定义数组，存放10万条数据
    for (let i = 0; i < 100000; i++) {
      // 循环添加10万条数据
      arr.push({
        id: i + 1,
        name: "名字" + (i + 1),
        value: i + 1,
        // imgSrc: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png'
      });
    }

    res.send({
      code: 0,
      msg: "成功",
      data: arr,
    });
  });
}
module.exports = getFileInterface;
